<?php
# =====================================
# 作者：老曹  QQ:1484743
# 版权声明：本程序为个人原创、开源程序，禁止用于任何违法用途，任何违法用途均与作者无关。
# 仅限学习交流，转载请注明出处。如需商用请联系作者。
# =====================================
require_once __DIR__ . '/../core/db.php';
header('Content-Type: text/plain');
$hostId = isset($_GET['hostId']) ? intval($_GET['hostId']) : 0;
if ($hostId <= 0) {
    http_response_code(400);
    echo 'hostId参数无效';
    exit;
}
$db = new DB();
$stmt = $db->prepare('SELECT token, os FROM hosts WHERE host_id = ?');
$stmt->bind_param('i', $hostId);
$stmt->execute();
$stmt->bind_result($token, $os);
if ($stmt->fetch()) {
    if (stripos($os, 'linux') !== false) {
        $script = file_get_contents(__DIR__ . '/../../scripts/template_linux.sh');
    } elseif (stripos($os, 'windows') !== false) {
        $script = file_get_contents(__DIR__ . '/../../scripts/template_windows.ps1');
    } elseif (stripos($os, 'mac') !== false) {
        $script = file_get_contents(__DIR__ . '/../../scripts/template_macos.sh');
    } else {
        $script = '# 暂不支持该操作系统';
    }
    $script = str_replace(['{{HOST_ID}}', '{{TOKEN}}'], [$hostId, $token], $script);
    echo $script;
} else {
    echo '# hostId不存在';
}
$stmt->close();
$db->close(); 